package sample;

import cn.hutool.json.JSONUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import sample.bdUtil.DbUtil;
import sample.data.IDCardData.IDCardData;
import sample.data.PhoneNumber.PhoneNumberData;
import sample.data.Video.VideoData;
import sample.util.HttpClient;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Controller {
    public Label videoLabel;
    public TextField videotext;
    public TextArea videoTextArea;
    public TextField idCardtext;
    public Label idCardLabel;
    public TextArea idCardTextArea;
    public TextField phoneNumbertext;
    public Label phoneNumberLabel;
    public TextArea phoneNumberTextArea;
    public Label dbText;


    @FXML
    protected void onVideoButtonClick(){
        String video = videotext.getText();
        String url="https://qqlykm.cn/api/duanshipin/get?key=DX6ydVAWmKw2auQeBO6eyCANNq&url="+video;
        String data= HttpClient.Get(url);
        VideoData videoData= JSONUtil.toBean(data,VideoData.class);
        String msg=videoData.getMessage();
        String dis="无水印视频封面图片链接:\n"+videoData.getData().getImg();
        dis+="\n无水印视频下载链接：\n"+videoData.getData().getVideo();
        videoLabel.setText(msg);
        videoTextArea.setWrapText(true);
        videoTextArea.setText(dis);

    }
    @FXML
    protected void onidCardButtonClick(){
        String idCard=idCardtext.getText();
        String url="https://widget.cifuwu.com/idcard/"+idCard;
        String data=HttpClient.Get(url);
        IDCardData idCardData=JSONUtil.toBean(data,IDCardData.class);
        String dbName = "test"; //数据库名
        String connUrl = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&charset=utf8"; //JDBC链接，需要MySQLConnector库支持
        String dbUser = "root";//数据库账户
        String dbPwd = "xfg2580.";//数据库密码
        Connection conn = DbUtil.getMySQLConnection(connUrl, dbUser, dbPwd);//获取数据库连接句柄
        String sqlQuery = "select * from address where class=?";//查询语句，使用preparedStatement构建
        Object[] paramsWhere = {idCard}; //preparedStatement 参数，相当于id=1 or typename='陶瓷大学'
        //上面的sqlQuery和paramsWhere相当于：select * from test where id=1 or typename='陶瓷大学'
        String sqlInsert = "insert into address (class,name) values (?,?)";//插入语句，使用preparedStatement构建
        Object[] paramsInsert = {idCard,idCardData.getData().getAreaInfo().getDetail()};//preparedStatement 参数，相当于 values('陶瓷大学')
        //上面的sqlInsert和paramsInsert相当于：insert into test (typename) values ('陶瓷大学')
        ResultSet rs = DbUtil.executeQuery(conn, sqlQuery, paramsWhere);

        String msg="";
        try {
            while (rs.next()){
                msg ="户籍所在地："+idCardData.getData().getAreaInfo().getDetail()+"\n";
                msg+="年龄："+idCardData.getData().getBorn().getAge()+"\n";
                msg+="性别："+idCardData.getData().getGender().getCn()+"\n";
                msg+="生日（公历）："+idCardData.getData().getBorn().getSolar().getYear()+"-"+idCardData.getData().getBorn().getSolar().getMonth()+"-"+idCardData.getData().getBorn().getSolar().getDate()+"\n";
                msg+="生日（农历）："+idCardData.getData().getBorn().getLunar().getChinese_birthday()+"\n";
                msg+="星座："+idCardData.getData().getBorn().getSolar().getZodiac()+"\n";
                msg+="生肖："+idCardData.getData().getBorn().getLunar().getChinese_zodiac()+"\n";
            }if (msg==""){
                DbUtil.executeUpdate(conn, sqlInsert, paramsInsert);
                System.out.println("新增成功");
                msg ="户籍所在地："+idCardData.getData().getAreaInfo().getDetail()+"\n";
                msg+="年龄："+idCardData.getData().getBorn().getAge()+"\n";
                msg+="性别："+idCardData.getData().getGender().getCn()+"\n";
                msg+="生日（公历）："+idCardData.getData().getBorn().getSolar().getYear()+"-"+idCardData.getData().getBorn().getSolar().getMonth()+"-"+idCardData.getData().getBorn().getSolar().getDate()+"\n";
                msg+="生日（农历）："+idCardData.getData().getBorn().getLunar().getChinese_birthday()+"\n";
                msg+="星座："+idCardData.getData().getBorn().getSolar().getZodiac()+"\n";
                msg+="生肖："+idCardData.getData().getBorn().getLunar().getChinese_zodiac()+"\n";
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        idCardTextArea.setText(msg);
    }

    @FXML
    protected void onphoneNumberButtonClick() {
        String phoneNumber = phoneNumbertext.getText();
        String url = "https://qqlykm.cn/api/hmjx/get?key=DX6ydVAWmKw2auQeBO6eyCANNq&number=" + phoneNumber;
        String data = HttpClient.Get(url);
        PhoneNumberData phoneNumberData = JSONUtil.toBean(data, PhoneNumberData.class);
        String dbName = "test"; //数据库名
        String connUrl = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&charset=utf8"; //JDBC链接，需要MySQLConnector库支持
        String dbUser = "root";//数据库账户
        String dbPwd = "xfg2580.";//数据库密码
        Connection conn = DbUtil.getMySQLConnection(connUrl, dbUser, dbPwd);//获取数据库连接句柄
        String sqlQuery = "select * from address where class=?";//查询语句，使用preparedStatement构建
        Object[] paramsWhere = {phoneNumber}; //preparedStatement 参数，相当于id=1 or typename='陶瓷大学'
        //上面的sqlQuery和paramsWhere相当于：select * from test where id=1 or typename='陶瓷大学'
        String sqlInsert = "insert into address (class,name) values (?,?)";//插入语句，使用preparedStatement构建
        Object[] paramsInsert = {phoneNumber,phoneNumberData.getData().getProvince()};//preparedStatement 参数，相当于 values('陶瓷大学')
        //上面的sqlInsert和paramsInsert相当于：insert into test (typename) values ('陶瓷大学')
        ResultSet rs = DbUtil.executeQuery(conn, sqlQuery, paramsWhere);

        String msg="";
        try {
            while (rs.next()) {
                msg="号码归属地："+ phoneNumberData.getData().getProvince()+ phoneNumberData.getData().getCity()+"("+phoneNumberData.getData().getOperator()+")";
                msg+="\n信息分析："+phoneNumberData.getData().getAnalysisDetail();
                msg+="\n特性分析："+phoneNumberData.getData().getCharacterAnalysis();
                msg +="\n运势："+ phoneNumberData.getData().getDestinyDetail();
            }
            if (msg == "") {
                DbUtil.executeUpdate(conn, sqlInsert, paramsInsert);
                System.out.println("新增成功");
                msg="号码归属地："+ phoneNumberData.getData().getProvince()+ phoneNumberData.getData().getCity()+"("+phoneNumberData.getData().getOperator()+")";
                msg+="\n信息分析："+phoneNumberData.getData().getAnalysisDetail();
                msg+="\n特性分析："+phoneNumberData.getData().getCharacterAnalysis();
                msg +="\n运势："+ phoneNumberData.getData().getDestinyDetail();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        phoneNumberTextArea.setWrapText(true);
        phoneNumberTextArea.setText(msg);


    }
//    @FXML
//    protected void onDbRead() {
//        String dbName = "test"; //数据库名
//        String connUrl = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&charset=utf8"; //JDBC链接，需要MySQLConnector库支持
//        String dbUser = "root";//数据库账户
//        String dbPwd = "xfg2580.";//数据库密码
//        Connection conn = DbUtil.getMySQLConnection(connUrl, dbUser, dbPwd);//获取数据库连接句柄
//        String sqlQuery = "select * from address where id=? or class=?";//查询语句，使用preparedStatement构建
//        Object[] paramsWhere = {1, "陶瓷大学"}; //preparedStatement 参数，相当于id=1 or typename='陶瓷大学'
//        //上面的sqlQuery和paramsWhere相当于：select * from test where id=1 or typename='陶瓷大学'
//        String sqlInsert = "insert into address (class) values (?)";//插入语句，使用preparedStatement构建
//        Object[] paramsInsert = {"陶瓷大学"};//preparedStatement 参数，相当于 values('陶瓷大学')
//        //上面的sqlInsert和paramsInsert相当于：insert into test (typename) values ('陶瓷大学')
//        ResultSet rs = DbUtil.executeQuery(conn, sqlQuery, paramsWhere);
//        try {
//            while (rs.next()) {
//                System.out.print(rs.getInt("id") + ":");
//                System.out.println(rs.getString("class"));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        DbUtil.executeUpdate(conn, sqlInsert, paramsInsert);
//    }
}

